<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2022/05/20
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\store;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
use ImiApp\ApiServer\Api\Controller\Tpmecms;

use ImiApp\ApiServer\Api\Model\Store;
use phpDocumentor\Reflection\Types\Object_;

/**
 * 店主
 * @Auth(name="token",nologin={"no"})
 */
class StoreOwner extends StoreBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];

    public $store;
    public $store_id;
    protected $whereCom;

    public function __init()
    {
        var_dump('StoreOwner __init');
        // parent::_initialize();
        // $this->store = $this->getStoreByUserId();
        // $this->store_id = $this->getStoreId();
        // $this->AddStoreLog($this->store_id);
    }

    public function init()
    {
        $this->store = (object)$this->isStore($this->auth->user()->id);
        $this->store_id = $this->store->id;
        $this->whereCom['store_id'] = $this->store_id;
    }



    /**
     * 是否到期
     *
     * @return boolean
     */
    public function isRun()
    {
        if ($this->store->expire_time < time())
            return $this->error('店铺认证时间到期', $this->store->expire_time, CODE_店铺认证时间到期);
    }

    /**
     * 是否可以添加发布商品
     *
     * @return boolean
     */
    public function isAddGoods()
    {
        //用:1=平台提现,2=执行分账 
        if ($this->store->money_status == '2') {
            //有没有支付保证金 不能小于设置的保证金
            $surety = $this->GetStoreSuretyMoney($this->store->set_id);
            if ($this->store->surety < $surety) {
                return $this->error('店铺保证金不足' . $surety, $this->store->surety, CODE_店铺保证金不足);
            }
        }
    }


    /**
     * 是否可以发布活动
     *
     * @return boolean
     */
    public function isAddAct()
    {
        //用:1=平台提现,2=执行分账 
        if ($this->store->money_status == '2') {
            //有没有支付保证金 不能小于设置的保证金
            $surety = $this->GetStoreSuretyMoney($this->store->set_id);
            if ($this->store->act_surety < $surety) {
                return $this->error('店铺活动保证金不足' . $surety, $this->store->act_surety, CODE_店铺活动保证金不足);
            }
        }
    }


    /**
     * 获取店铺id
     * 
     * @return void
     */
    public function getStoreId()
    {
        return $this->store->id;
    }

    /**
     * 获取店铺
     * 
     * @param int $user_id
     * @return void
     */

    public function getStoreByUserId($user_id = null)
    {
        var_dump('$this->auth->user()->id', $$this->auth->user()->id);
        if (!$user_id)
            $user_id = $this->auth->user()->id;

        // $store = Db::name('store')->where('user_id', $user_id)->select()->get();
        $store = Store::dbQuery()->where('user_id', '=', $user_id)->find();
        if (!$store) {
            return $this->error('无权限,您不是店铺哦!', null, CODE_非店铺);
        }
        return $store;
    }



    /**
     * 判断是否可以发布某个分类下的商品
     * 
     * @param string $goodscate_ids 商品分类ids
     * @return void
     */
    protected function isGoodscateIds($goodscate_ids)
    {
        $this->store = (object)$this->isStore($this->auth->user()->id);
        //获取个人主分类下的所有子分类
        $goodscateArr = $this->GetGoodscateSub($this->store->goodscate_ids);

        if (!in_array($goodscate_ids, $goodscateArr)) {
            return $this->error('您没有上传该商品分类资格', $goodscateArr, CODE_您没有上传该商品分类资格);
        }
    }
}
